package com.maowu.pooling.database;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.maowu.pooling.ConnectionDamagedException;
import com.maowu.pooling.ConnectionFactory;
import com.maowu.pooling.FailedToConnectException;

public class PoolingDataSource implements DataSource
{
    private DataSource originalDataSource;
    
    private class MyFactory implements ConnectionFactory
    {
        public Object createConnection(long connectTimeout)
                throws FailedToConnectException
        {
            try
            {
                int seconds = (int) ((connectTimeout + 500)/1000);
                if (seconds == 0 && connectTimeout > 0)
                {
                    seconds = 1;
                }
                
                synchronized(this)
                {
                    originalDataSource.setLoginTimeout(seconds);
                    return originalDataSource.getConnection();
                }
            }
            catch (SQLException e)
            {
                throw new FailedToConnectException(e);
            }
        }

        public void destroyConnection(Object conn)
        {
            try
            {
                ((Connection)conn).close();
            }
            catch (SQLException e)
            {
                try
                {
                    e.printStackTrace(originalDataSource.getLogWriter());
                }
                catch (SQLException e1)
                {
                    e.printStackTrace();
                    e1.printStackTrace();
                    
                }
            }
            
        }

        public void touchConnection(Object conn, long connectTimeout)
                throws FailedToConnectException, ConnectionDamagedException
        {
            Connection c = (Connection) conn;
            //FIXME how to determine ?? 
            
        }
    }
    

    public PoolingDataSource(DataSource originalDataSource)
    {
        super();
        if(originalDataSource == null)
        {
            throw new NullPointerException("originalDataSource can not be null.");
        }
        this.originalDataSource = originalDataSource;
        
        
    }


    public DataSource getOriginalDataSource()
    {
        return originalDataSource;
    }


    public Connection getConnection() throws SQLException
    {
        // TODO Auto-generated method stub
        return null;
    }

    public Connection getConnection(String username, String password)
            throws SQLException
    {
        // TODO Auto-generated method stub
        return null;
    }

    public PrintWriter getLogWriter() throws SQLException
    {
        // TODO Auto-generated method stub
        return null;
    }

    public int getLoginTimeout() throws SQLException
    {
        // TODO Auto-generated method stub
        return 0;
    }

    public void setLogWriter(PrintWriter out) throws SQLException
    {
        // TODO Auto-generated method stub
        
    }

    public void setLoginTimeout(int seconds) throws SQLException
    {
        // TODO Auto-generated method stub
        
    }
}
